﻿namespace Vdc.Grid
{
    using System;
    using System.IO;
    using System.Web.UI.Design;
    using System.Windows.Forms;

    internal class GridIOHelper
    {
        public static string EvaluateRelativePath(string pathToFile, string pathToSpecialFolder, string specialFolderName)
        {
            int num2;
            string str = "";
            pathToFile = pathToFile.Replace("/", @"\").Trim();
            pathToSpecialFolder = pathToSpecialFolder.Replace("/", @"\").Trim();
            if ((pathToFile.Length > 0) && (pathToFile.LastIndexOf(@"\") == (pathToFile.Length - 1)))
            {
                pathToFile = pathToFile.Substring(0, pathToFile.Length - 1);
            }
            if ((pathToFile.Length > 0) && (pathToFile.LastIndexOf(".") > pathToFile.LastIndexOf(@"\")))
            {
                pathToFile = pathToFile.Substring(0, pathToFile.LastIndexOf(@"\"));
            }
            if ((pathToSpecialFolder.Length > 0) && (pathToSpecialFolder.LastIndexOf(@"\") == (pathToSpecialFolder.Length - 1)))
            {
                pathToSpecialFolder = pathToSpecialFolder.Substring(0, pathToSpecialFolder.Length - 1);
            }
            pathToFile = (pathToFile.Substring(0, 1) == @"\") ? pathToFile.Substring(1) : pathToFile;
            string[] strArray = (pathToFile.Length > 0) ? pathToFile.Split(new char[] { '\\' }) : new string[0];
            pathToSpecialFolder = (pathToSpecialFolder.Substring(0, 1) == @"\") ? pathToSpecialFolder.Substring(1) : pathToSpecialFolder;
            string[] strArray2 = (pathToSpecialFolder.Length > 0) ? pathToSpecialFolder.Split(new char[] { '\\' }) : new string[0];
            if ((((strArray.Length > 0) && (strArray[0].IndexOf(":") > 0)) || ((strArray2.Length > 0) && (strArray2[0].IndexOf(":") > 0))) && (strArray[0].ToLower() != strArray2[0].ToLower()))
            {
                MessageBox.Show("Project folder and " + specialFolderName + " need to be on the same drive.");
                return "";
            }
            int index = 0;
            while (((index < strArray.Length) && (index < strArray2.Length)) && (strArray[index].ToLower() == strArray2[index].ToLower()))
            {
                index++;
            }
            for (num2 = 0; num2 < (strArray.Length - index); num2++)
            {
                str = str + "../";
            }
            for (num2 = index; num2 < strArray2.Length; num2++)
            {
                if (num2 > index)
                {
                    str = str + "/";
                }
                str = str + strArray2[num2];
            }
            return str;
        }

        public static string GetPathToActiveFile(Vdc.Grid.Grid _component)
        {
            try
            {
                IWebFormsDocumentService service = _component.Site.GetService(typeof(IWebFormsDocumentService)) as IWebFormsDocumentService;
                return Path.GetFullPath(service.DocumentUrl.Replace("file:///", ""));
            }
            catch (Exception exception1)
            {
                if (exception1 != null)
                {
                }
                return "";
            }
        }

        public static string GetRelativePathToProject(Vdc.Grid.Grid _component, string relativePath)
        {
            try
            {
                IWebApplication service = (IWebApplication) _component.Site.GetService(typeof(IWebApplication));
                return service.GetProjectItemFromUrl(relativePath).PhysicalPath;
            }
            catch (Exception exception1)
            {
                if (exception1 != null)
                {
                }
                return "";
            }
        }
    }
}

